#coding:utf-8

import pandas as pd
import numpy as np

from test1 import Data_pre_processing
from test1 import relevance_analysis
from test import LightGBMTrainer
from test import LightGBMPredictor
from test2 import strategy
from test1 import data_time_handle

#数据读取
df1=pd.read_excel('1.xlsx')
df1=df1.rename(columns={'finish_time': 'data_date'})
df001=data_time_handle.extract_all_date_features(df1[['data_date']],date_column='data_date')
df1=pd.merge(df1,df001,on=['data_date'])

#时间格式
df1['data_date'] = pd.to_datetime(df1['data_date'],format='%Y-%m-%d')
df1['date']=df1['data_date'].dt.strftime('%Y%m%d')
df1['date'] = df1['date'].astype(int)
#强制转换类型
#df1=df1.astype(float)
#数据清洗
df1['value'] = df1['value'].replace(['', 'NA', 'NaN', 'null', None, np.nan], 0)
#df2=Data_pre_processing.washing(df1,10,0)
#df2=Data_pre_processing.washing(df2,20,0)
df2=df1.copy()
# if len(df2.columns.tolist())<=10:
#     df3=df2
# else:
#     #相关系数计算
#     df21=relevance_analysis.correlation_coefficient(df2,'kendall')
#     df22=df21[abs(df21)>0.5]
#     #df01=df01.loc[(df01['yc_date']<now_date2)]
#     list_index=df22.index.tolist()
#     df3=df2[list_index]

# #归一化
# df4,list_range,list_min=Data_pre_processing.noramlization_training(df3)

# for i in range(len(list_range)):
#     if abs(list_min[i]/list_range[i])<=0.01:
#         normalization=0
#         break
#     else:
#         normalization=1

# #是否启用归一化
# if normalization==1:
#     zero_cols = [col for col in df4.columns if (df4[col] == 0).all()]
#     df5 = df4.drop(columns=zero_cols)
#     zero_cols = [col for col in df4.columns if (df5[col] == 1).all()]
#     df5 = df5.drop(columns=zero_cols)
# else:
#     df5=df3

df5=df2.copy()

#模型训练
del df5['data_date']
a01='value'
list5_columns=df5.columns.tolist()
list51=list5_columns.copy()
list51.remove(a01)
X=df5[list51] 
y=df5[a01] 

#超参的设定
params = {'learning_rate': 0.005,
                'n_estimators': 1000,
                'min_data_in_leaf':20,
                'max_depth':8,
                'num_leaves': 100,
                'subsample':1,
                'colsample_bytree':0.8,

                'lambda_l1':0.1,
                'lambda_l2':0.2,

                'boosting_type': 'gbdt',
                'objective':'regression',
                'metric': ['huber', 'rmse'],
                #'best_model':'huber',       #输出最佳模型的数值，目前未启用
                
                #'device':'gpu'
                #'gpu':4, 
                #'gpu_platform_id':2,
                #'gpu_device_id':1
                }
trainer = LightGBMTrainer(params)
model = trainer.train(X, y)
trainer.save_model("model_test.txt")
print("Training complete!")

#预测数据
df01=pd.read_excel('2.xlsx')
df01=df01.rename(columns={'finish_time': 'data_date'})
df001=data_time_handle.extract_all_date_features(df01[['data_date']],date_column='data_date')
df01=pd.merge(df01,df001,on=['data_date'])
#时间格式
df01['data_date'] = pd.to_datetime(df01['data_date'],format='%Y-%m-%d')
df01['date']=df01['data_date'].dt.strftime('%Y%m%d')
df01['date'] = df01['date'].astype(int)
df02=df01.copy()
del df02['data_date']
print(df02)
#强制转换类型
#df01=df01.astype(float)

# #数据处理
# if normalization==1:
#     df02=Data_pre_processing.noramlization_predict(df01,list_range,list_min)
# else:
#     df02=df01
df03=df02[list51]
predictor = LightGBMPredictor("model_test.txt")
predictions = predictor.predict(df03.values.tolist())

# #数据恢复
# if normalization==1:
#     y_pred=predictions*list_range[-1]+list_min[-1]
# else:
#     y_pred=predictions
y_pred=predictions.copy()
#数据后处理
y_pred=strategy.Nonnegative_number(y_pred)

df04=pd.DataFrame(data=y_pred,columns=['values'])

#数据导出
df04.to_excel('3.xlsx',index=False)
